package com.eece496.Server;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.eece496.Database.COGSAssignments;
import com.eece496.Database.Grades;
import com.eece496.Database.StudentGroups;
import com.eece496.Helpers.AssignmentInfo;
import com.eece496.Helpers.GradesInfo;

/**
 * Servlet implementation class ProfEditGrades
 */
public class ProfEditGrades extends MasterServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ProfEditGrades() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String[] valueArray = request.getParameterValues("Grades");  
		String courseNumber = request.getParameter("courseNumber");
		String sectionNumber = request.getParameter("sectionNumber");
		String firstname = request.getParameter("firstname");
		String lastname = request.getParameter("lastname");
	
		Grades GradesDB = new Grades();
		GradesDB.Setup(connection);
		ArrayList<GradesInfo> GradesInfo = new ArrayList<GradesInfo>();
		ArrayList<String> AssignmentInfo = new ArrayList<String>();
		ArrayList<AssignmentInfo> aInfo = new ArrayList<AssignmentInfo>();
		COGSAssignments AssignmentDB = new COGSAssignments();
		AssignmentDB.Setup(connection);
	
		try {
			GradesInfo = GradesDB.GetAllGradesFromCourse(courseNumber, sectionNumber);
			AssignmentInfo = GradesDB.GetAssignments(courseNumber, sectionNumber);

		
		System.out.println(GradesInfo.size());
		System.out.println(AssignmentInfo.size());
		rotateList(AssignmentInfo);
		
		int count = 0;
	   for(GradesInfo info: GradesInfo)
	   {
		   for(int i=0; i < info.grades.size(); i++)
		  {
			   System.out.println(valueArray[count]);
			   System.out.println("Grade in Database: " + info.grades.get(i) + " Grade in textfield: " + valueArray[count]);
			   if(!info.grades.get(i).equals(valueArray[count]))
			   {
				   //If the student was absent and got a mark of 0 they go up a priority
				   if(info.grades.get(i).equals("AB") && valueArray[count].equals("0"))
					{
					   StudentGroups StudentGroupsDB = new StudentGroups();
					   StudentGroupsDB.Setup(connection);
					   int priority = StudentGroupsDB.GetPriority(info.studentID, courseNumber, sectionNumber);
					   priority = priority + 1;
					   StudentGroupsDB.ModifyPriority(priority, info.studentID, courseNumber, sectionNumber);
					   
					}
				   GradesDB.ModifyGrade(valueArray[count], courseNumber, sectionNumber, info.studentID, AssignmentInfo.get(i));
			   }
			   count++;
		  }
	   }
		//System.out.println(valueArray.length);
		
	   	aInfo =  AssignmentDB.GetAssignmentInfo(courseNumber, sectionNumber);
		request.setAttribute("firstname", firstname);
		request.setAttribute("lastname", lastname);
		request.setAttribute("courseNumber", courseNumber);
		request.setAttribute("sectionNumber", sectionNumber);
		request.setAttribute("AssignmentInfo", AssignmentInfo);
		request.setAttribute("GradesInfo", GradesInfo);
		
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	    response.sendRedirect("/GroupAssessments/ProfViewGradesServlet?firstname="+firstname+"&lastname="+lastname+"&courseNumber="+courseNumber+"&sectionNumber="+sectionNumber);
		/*
		request.getRequestDispatcher("/Professor/Professor-View-Grades.jsp")
		.forward(request, response);
		*/

	}
	
	private void rotateList(ArrayList<String> list)
	{
		for(int i = (list.size()-1); i > 0; i--)
		{
			if(i == 0)
			{
				break;
			}
			String temp = list.get(i);
			String temp2 = list.get(i-1);

			list.set(i, temp2);
			list.set(i-1, temp);
		}
	}
}
